package com.dataaccess.dataaccess.web.web;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.dataaccess.dataaccess.common.base.BaseService;
import com.dataaccess.dataaccess.model.TestDevice;
import com.dataaccess.dataaccess.support.annotaion.Action;
import com.dataaccess.dataaccess.support.annotaion.At;
import com.dataaccess.dataaccess.support.filter.RequestWrapper;
import com.dataaccess.dataaccess.support.model.RespEntity;
import com.dataaccess.dataaccess.support.utils.requestutil.HttpRequestUtil;
import com.dataaccess.dataaccess.web.mapper.TestDeviceMapper;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: YangYu
 * @ClassName: TestDeviceController
 * @Date: Created in 15:55 2021/7/15
 * @Description: 设备对接测试（取数据）
 */
@Action
@RequestMapping("test")
public class TestDeviceController extends BaseService {
    @Resource
    TestDeviceMapper testDeviceMapper;
    /**
     * 回调地址：设备数据变化，订阅后推送设备数据变化通知
     * @param
     * @return
     */
    @At(value = "device", method = RequestMethod.POST)
    public RespEntity testDevice(@RequestBody(required = false) JSONObject jsonParam) {
        TestDevice testDevice = new TestDevice();
        testDevice.setId(UUID.randomUUID().toString());
        testDevice.setJsondata(jsonParam.toString());
        testDeviceMapper.insert(testDevice);
        return RespEntity.success();
    }
    @At(value = "/data", method = RequestMethod.POST)
    public RespEntity videoAlarmInfo(HttpServletRequest request) throws Exception {
        JSONObject jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        TestDevice testDevice = new TestDevice();
        testDevice.setId(UUID.randomUUID().toString());
        testDevice.setJsondata(jsonParam.toString());
        testDeviceMapper.insert(testDevice);
        return RespEntity.success();
    }
    /**
     * 回调地址：设备数据变化，订阅后推送设备数据变化通知
     * @param
     * @return
     */
    @At(value = "requestDevice", method = RequestMethod.POST)
    public RespEntity testDevice(@RequestParam Map<String, Object> params, HttpServletRequest request) throws IOException {
        JSONObject jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        System.out.println("++++++"+jsonParam);
        System.out.println(jsonParam.getStr("url"));
//        TestDevice testDevice = new TestDevice();
//        testDevice.setId(UUID.randomUUID().toString());
//        testDevice.setJsondata(jsonParam.toString());
//        testDeviceMapper.insert(testDevice);
        //testService.test(jsonParam,request);
        BufferedReader br = request.getReader();
        String str, wholeStr = "";
        while((str = br.readLine()) != null){
            wholeStr += str;
        }
        JSONObject jsonObject = new JSONObject(wholeStr);
        System.out.println(jsonObject);
        return RespEntity.success();
    }

    /**
     * @Author YangYu
     * @Description  接受jsonArray数据
     * @Date Created in 14:42 2023/5/29
     * @Param [jsonArray]
     */
    @At(value = "jsonArray", method = RequestMethod.POST)
    public JSONObject testDevice(@RequestBody(required = false) JSONArray jsonArray) {
        TestDevice testDevice = new TestDevice();
        testDevice.setId(UUID.randomUUID().toString());
        testDevice.setJsondata(jsonArray.toString());
        testDeviceMapper.insert(testDevice);
        JSONObject ret = new JSONObject();
        ret.put("code", 0);
        ret.put("msg", "接收成功");
        return ret;
    }
    /**
     * @Author YangYu
     * @Description  接受request数据
     * @Date Created in 14:42 2023/5/29
     * @Param [jsonArray]
     */
    @At(value = "/request", method = RequestMethod.POST)
    public JSONObject request(HttpServletRequest request) throws Exception {
        JSONObject jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        TestDevice testDevice = new TestDevice();
        testDevice.setId(UUID.randomUUID().toString());
        if (jsonParam.size() == 0){
            BufferedReader br = request.getReader();
            String str, wholeStr = "";
            while((str = br.readLine()) != null){
                wholeStr += str;
            }
            testDevice.setJsondata(wholeStr);
        }else {
            testDevice.setJsondata(jsonParam.toString());
        }
        testDeviceMapper.insert(testDevice);
        JSONObject ret = new JSONObject();
        ret.put("code", 0);
        ret.put("msg", "成功");
        return ret;
    }

}
